/**
 *  === 时间格式 ===
 * 	date: 时间
 * 	type：显示的时间格式
 * 	无参数 返回当前时间
 **/
export function formatDate(date = null, type = null) {
  if (!date) {
    // date 没传值 默认 当前时间
    // let yy = new Date().getFullYear();
    // let mm = new Date().getMonth() + 1;
    // let dd = new Date().getDate();
    // let hh = new Date().getHours();
    // let mf = new Date().getMinutes() < 10 ? '0' + new Date().getMinutes() : new Date().getMinutes();
    // let ss = new Date().getSeconds() < 10 ? '0' + new Date().getSeconds() : new Date().getSeconds();

    // date = yy + '-' + mm + '-' + dd + ' ' + hh + ':' + mf + ':' + ss;
    return;
  }

  let datetime = new Date(date);

  let year = datetime.getFullYear();
  let month = datetime.getMonth() + 1;
  let day = datetime.getDate();
  let hours = datetime.getHours();
  let minutes = datetime.getMinutes();
  let second = datetime.getSeconds();

  let nowTime = +new Date();
  let distance = nowTime - datetime;

  let result;

  // 格式
  switch (type) {
    case "hh:mm": // 00:00
      result = fillZero(hours) + ":" + fillZero(minutes);
      break;
    case "MM-dd": // 01-01
      result = fillZero(month) + "-" + fillZero(day);
      break;
    case "MM-dd hh:mm": // 01-01 00:00
      result =
        fillZero(month) +
        "-" +
        fillZero(day) +
        " " +
        fillZero(hours) +
        ":" +
        fillZero(minutes);
      break;
    case "yyyy-MM-dd": // 2016-01-05
      result = year + "-" + fillZero(month) + "-" + fillZero(day);
      break;
    case "yyyy-MM-dd hh:mm": // 2016-01-01 00:00
      result =
        year +
        "-" +
        fillZero(month) +
        "-" +
        fillZero(day) +
        " " +
        fillZero(hours) +
        ":" +
        fillZero(minutes);
      break;
    case "after": // 当前时间-- 秒 分 时 天 月 年
      if (distance <= 60 * 1000) {
        // result = Math.floor((nowTime - datetime) / 1000) + "秒前";
        result = "刚刚";
      } else if (distance <= 3600 * 1000) {
        result = Math.floor((nowTime - datetime) / 60000) + "分钟前";
      } else if (distance <= 86400 * 1000) {
        result = Math.floor((nowTime - datetime) / 3600000) + "小时前";
      } else if (distance < 86400000 * 30) {
        result = Math.floor((nowTime - datetime) / 86400000) + "天前";
      } else if (distance < 86400000 * 30 * 12) {
        result = Math.floor((nowTime - datetime) / 86400000 / 30) + "个月前";
      } else {
        result = "一年前";
      }
      break;
    case "step": // 当前时间-- 刚刚  时:分  月:日  年-月-日
      if (distance <= 60 * 1000) {
        // result = Math.floor((nowTime - datetime) / 1000) + "秒前";
        result = "刚刚";
      } else if (distance <= 3600 * 1000) {
        result = Math.floor((nowTime - datetime) / 60000) + "分钟前";
      } else if (distance <= 86400 * 1000) {
        result = fillZero(hours) + ":" + fillZero(minutes);
      } else if (distance < 86400000 * 30) {
        result = fillZero(month) + "-" + fillZero(day);
      } else {
        result = year + "-" + fillZero(month) + "-" + fillZero(day);
      }
      break;
    default: // 2016-01-01 00:00:00
      result =
        year +
        "-" +
        fillZero(month) +
        "-" +
        fillZero(day) +
        " " +
        fillZero(hours) +
        ":" +
        fillZero(minutes) +
        ":" +
        fillZero(second);
  }

  return result;
}
function fillZero(value) {
  return value >= 10 ? value : "0" + value;
}

export function loadFile(name) {
  // name为文件所在位置
  let xhr = new XMLHttpRequest();
  const okStatus = document.location.protocol === "file:" ? 0 : 200;
  xhr.open("GET", name, false);
  xhr.overrideMimeType("text/html;charset=utf-8"); //默认为utf-8
  xhr.send(null);
  return xhr.status === okStatus ? xhr.responseText : null;
}

export function getDayCount(startDate, endDate) {
  const oneDay = 24 * 60 * 60 * 1000; // 每天的毫秒数
  const start = new Date(startDate);
  const end = new Date(endDate);
  const days = (end - start) / oneDay;
  return Math.round(days);
}

/**
 * @description 判断是否是数组，且数组长度不为 0
 * @param {Any} arr
 */
export function isValidArray(arr) {
  return !!(Array.isArray(arr) && arr.length);
}
export function checkTime(val) {
  let vals = val
  vals = new Date(new Date(vals).setDate(new Date(vals).getDate() - 1))
  // return [new Date(vals).getFullYear() + '-' + (new Date(vals).getMonth() + 1 > 9 ? new Date(vals).getMonth() + 1 : '0'
  //   + (new Date(vals).getMonth() + 1)) + '-' +
  //   (new Date(vals).getDate() > 9 ? new Date(vals).getDate() : '0' + new
  //     Date(vals).getDate()),
  // new Date(val).getFullYear() + '-' + (new Date(val).getMonth() + 1 > 9 ? new Date(val).getMonth() + 1 : '0'
  //   + (new Date(val).getMonth() + 1)) + '-' +
  // (new Date(val).getDate() > 9 ? new Date(val).getDate() : '0' + new
  //   Date(val).getDate())]
  return [new Date(val).getFullYear() + '-' + (new Date(val).getMonth() + 1 > 9 ? new Date(val).getMonth() + 1 : '0'
    + (new Date(val).getMonth() + 1)) + '-' +
    (new Date(val).getDate() > 9 ? new Date(val).getDate() : '0' + new
      Date(val).getDate()),
  new Date(val).getFullYear() + '-' + (new Date(val).getMonth() + 1 > 9 ? new Date(val).getMonth() + 1 : '0'
    + (new Date(val).getMonth() + 1)) + '-' +
  (new Date(val).getDate() > 9 ? new Date(val).getDate() : '0' + new
    Date(val).getDate())]
}

export function toLogin() {
  localStorage.clear();
  setTimeout(() => {
    window.location.href = '/dmznt-web/#/login'  // 跳登录
    // window.location.href = process.env.NODE_ENV !== "development" ? '/dmznt-web/#/login' : '/#/login'  // 跳登录
  }, 2000)
}


export function getCookie(name) {
  var cookieString = document.cookie;
  var cookies = cookieString.split("; ");
  for (var i = 0; i < cookies.length; i++) {
    var cookie = cookies[i].split("=");
    if (decodeURIComponent(cookie[0]) === name) {
      return decodeURIComponent(cookie[1]);
    }
  }
  return "";
}
export function setCookie(name, value) {
  document.cookie = `${name}=${value}`
  console.log('document.cookie', document.cookie)
}
export function removeCookie(name) {
  setCookie(name, '')
}

export function formatTimestamp(timestamp) {
  // 将字符串拆分为日期和时间部分
  const year = timestamp.substring(0, 4);
  const month = timestamp.substring(4, 6);
  const day = timestamp.substring(6, 8);
  const hour = timestamp.substring(8, 10);
  const minute = timestamp.substring(10, 12);
  const second = timestamp.substring(12, 14);

  // 组合成标准格式
  return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
}
// export function getStatus(num) {
//   switch (num) {
//     case 0:
//       return { text: '草稿', color: '#767676' }
//     case 1:
//       return { text: '审核中', color: '#47A0FF' }
//     case 2:
//       return { text: '已退回', color: '#F56C6C' }
//     case 3:
//       return { text: '待开始', color: '#FF7D00' }
//     case 4:
//       return { text: '执行中', color: '#01C797' }
//     case 5:
//       return { text: '已结束', color: '#767676' }
//     case 6:
//       return { text: '已作废', color: '#767676' }
//     case 7:
//       return { text: '已删除', color: '#F56C6C' }
//     default:
//       return { text: '', color: '' }
//   }
// }
export function getStatus() {
  return [
    { num: 0, text: '草稿', color: '#767676' },
    { num: 1, text: '审核中', color: '#47A0FF' },
    { num: 2, text: '已退回', color: '#F56C6C' },
    { num: 3, text: '待开始', color: '#FF7D00' },
    { num: 4, text: '执行中', color: '#01C797' },
    { num: 5, text: '已结束', color: '#767676' },
    { num: 6, text: '已作废', color: '#767676' },
    { num: 7, text: '已删除', color: '#F56C6C' },
    { num: -1, text: '', color: '' }
  ]
}
export function hexToRgba(hex, alpha = 1) {
  const [r, g, b] = hex.match(/\w\w/g).map(x => parseInt(x, 16));
  return `rgba(${r}, ${g}, ${b}, ${alpha})`;
}